/*
 * @Author: 肖思汗 
 * @Date: 2025-06-25 16:01:08 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2025-09-05 17:00:40
 */

import { get, isEmpty } from "lodash";
import { autorun, makeAutoObservable } from "mobx";
import { MaterialCategoryData, StickerData } from "types/type";
import stickerModelApi from "./stickerModelApi";
import userStore from "src/pages/User/userStore";

export interface Option {
    value: number;
    label: string;
    children?: Option[];
}

class StickerModelStore {
    constructor() {
        makeAutoObservable(this, {}, { deep: false });
    }

    show = false;
    setShow(show: boolean) {
        this.show = show;
        if (this.show && isEmpty(this.cascaderOptions)) {
            this.getStickerGroup();
        }
    }

    //贴纸的分组
    cascaderOptions: Array<Option> = [];
    async getStickerGroup() {
        console.log("请求贴纸组");
        const result = await stickerModelApi.getMaterialCategoryTree()
        const cascaderOptions: Array<Option> =
            (result.rows as Array<MaterialCategoryData>)
                .filter(g => g.parentId === 0)
                .map(g => ({
                    value: g.materialCategoryId,
                    label: g.categoryName,
                    children: (g.children || []).map(c => ({
                        value: c.materialCategoryId,
                        label: c.categoryName
                    }))
                }));

        this.cascaderOptions = cascaderOptions;
        this.setActiveid([
            get(cascaderOptions, [0, "value"]) as number,
            get(cascaderOptions, [0, "children", 0, "value"]) as number
        ]);
    }

    // 当前选中的分组    activeid: [0, 0],
    activeid: [number, number | undefined] = [0, 0];
    setActiveid(activeid: [number, number | undefined]) {
        this.activeid = activeid;
        this.getNextPage(true);
    }

    //加载中
    loading = false;
    setLoading(loading: boolean) {
        this.loading = loading;
    }
    page = 1;
    pageSize: number = 16;
    hasNext: boolean = true;
    // 素材列表
    stickers: Array<StickerData> = [];
    async getNextPage(reset: boolean = false) {
        if (reset) {
            this.page = 1;
            this.stickers = [];
            this.hasNext = true;
        } else if (!this.hasNext || this.loading) {
            return;
        } else {
            this.page++;
        }
        this.loading = true;

        if (!this.activeid[1]) {
            this.hasNext = false;
            this.stickers = [];
            return;
        }

        const result = await stickerModelApi.getMaterialsList(
            this.page,
            this.pageSize,
            this.activeid[1],
        );

        if (result?.rows) {
            if (reset) {
                this.stickers = [];
                await new Promise(resolve => requestAnimationFrame(resolve));
                await new Promise(resolve => requestAnimationFrame(resolve));
                await new Promise(resolve => requestAnimationFrame(resolve));
                this.stickers = [...result.rows];
            } else {
                this.stickers = [...this.stickers, ...result.rows];
            }
            this.hasNext = (result.rows.length === this.pageSize);
            this.loading = false;
        }

    }


}

const stickerModelStore = new StickerModelStore();

export default stickerModelStore;

